package com.tools.util;


import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipUtil {

	/*
	 * 读取压缩包中的所有图片，并返回file集合
	 */

	public static List<File> zipFileRead(String file, String saveRootDirectory) {
		
		try{
			ZipFile zipFile =new ZipFile(file);
			Enumeration<ZipEntry> enu =(Enumeration<ZipEntry>)zipFile.entries();
			List<File> files=new ArrayList<File>();
			while(enu.hasMoreElements()){
				ZipEntry zipElement=(ZipEntry)enu.nextElement();
				InputStream read =zipFile.getInputStream(zipElement);
				String fileName =zipElement.getName();
				int index = fileName.lastIndexOf('.');
				
				
				if(index>-1){
					String extension = fileName.substring(index);
					String regexp = "^.(jpg|jpeg|png|gif)$";
					Pattern pattern = Pattern.compile(regexp);
					Matcher matcher = pattern.matcher(extension.toLowerCase());
					if(fileName !=null && matcher.matches()){
						File file2 = execute(zipElement,read,saveRootDirectory);
						files.add(file2);
					}
				}
				
				
				read.close();
			}
			return files;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * 解压图片并返回map
	 * @param file
	 * @param saveRootDirectory
	 * @return
	 */
	public static Map<String,File> zipFileReadToMap(String file, String saveRootDirectory) {

		try{
			ZipFile zipFile =new ZipFile(file, Charset.forName("gbk"));
			Enumeration<ZipEntry> enu =(Enumeration<ZipEntry>)zipFile.entries();
			Map<String,File> fileMap = new HashMap<>();
			while(enu.hasMoreElements()){
				ZipEntry zipElement=(ZipEntry)enu.nextElement();
				InputStream read =zipFile.getInputStream(zipElement);
				String fileName =zipElement.getName();
				int index = fileName.lastIndexOf('.');
				if(index>-1){
					String extension = fileName.substring(index);
					String regexp = "^.(jpg|jpeg|png|gif)$";
					Pattern pattern = Pattern.compile(regexp);
					Matcher matcher = pattern.matcher(extension.toLowerCase());
					if(fileName !=null && matcher.matches()){
						File file2 = execute(zipElement,read,saveRootDirectory);
						fileMap.put(file2.getName(),file2);
					}
				}
				read.close();
			}
			return fileMap;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public static File execute(ZipEntry ze, InputStream read,
			String saveRootDirectory) throws IOException{
		String fileName =ze.getName();
		
		//String[] filesName = split[split.length-2].split("-");
		
		File file =new File(saveRootDirectory+"/"+fileName);
		
		
		if(!file.exists()){
			File rootDirectoryFile =new File(file.getParent());
			if(!rootDirectoryFile.exists()){
				boolean ifSuccess =rootDirectoryFile.mkdirs();
				if(ifSuccess){
					System.out.println("文件夹创建成功！");
				} else {
					System.out.println("文件夹创建失败！");
				}
			}
			//创建文件
			try{
				file.createNewFile();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		//写入文件
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		BufferedOutputStream write =new BufferedOutputStream(fileOutputStream);
		int cha =0 ;
		while ((cha = read.read())!=-1){
			write.write(cha);
		}
		write.flush();
		write.close();
		read.close();
		return file;
	}
}
